package com.edu.demo.controller;

import com.edu.demo.config.FaceConfig;
import com.edu.demo.config.OssConfig;
import com.edu.demo.entity.dto.FileUploadDTO;
import com.edu.demo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;

/**
 * 文件的上传或下载
 */
@Api(tags = "文件的上传下载")
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private OssConfig ossConfig;
    @Autowired
    private RedisTemplate redisTemplate;

    //上传文件接口
    @ApiOperation(value = "(通用)上传文件接口")
    @PostMapping("/upload")
    public Result<FileUploadDTO> uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("文件类型") String fileType) throws IOException {
        String path = ossConfig.putObject(file, fileType);
        String url = "https://"+ ossConfig.getBucketName()+"."+ ossConfig.getEndpoint()+"/"+path;
        return Result.success(new FileUploadDTO(url,path)).msg("上传成功");
    }

    //下载文件接口
    @ApiOperation(value = "(通用)下载文件接口")
    @PostMapping("/download")
    public ResponseEntity<ByteArrayResource> downloadFile(@RequestParam("文件路径") String filePath) throws IOException {
        //输出流
        ByteArrayOutputStream outputStream = ossConfig.getObject(filePath);
        //定义一个响应头
        HttpHeaders httpHeaders = new HttpHeaders();

        //获取文件后缀
        String suffix = filePath.substring(filePath.lastIndexOf("."));
        //获取时间戳
        String time = String.valueOf(System.currentTimeMillis());
        filePath = "下崽神器_"+time+suffix;
        //设置下载文件名
        //中文名不乱码
        httpHeaders.setContentDispositionFormData("下崽神器_"+time,URLEncoder.encode(filePath, "utf-8"));
        httpHeaders.add("Cache-Control", "no-cache, no-store, must-revalidate");
        httpHeaders.add("Pragma", "no-cache");
        httpHeaders.add("Expires", "0");

        //设置响应内容为二进制
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ByteArrayResource byteArrayResource = new ByteArrayResource(outputStream.toByteArray());

        return ResponseEntity
                .ok()
                .headers(httpHeaders)
                .body(byteArrayResource);
    }

    //上传文件接口
    @ApiOperation(value = "头像上传")
    @PostMapping("/uploadHead")
    public Result<FileUploadDTO> uploadHead(@RequestParam("file") MultipartFile file) throws IOException {
        //判断上传的文件是图片类型
        if (!file.getContentType().contains("image")){
            return Result.error(400,"请上传图片");
        }
        String path = ossConfig.putObject(file, "headImg");
        String url = "https://"+ ossConfig.getBucketName()+"."+ ossConfig.getEndpoint()+"/"+path;
        redisTemplate.opsForHash().put("headImg", path,System.currentTimeMillis());
        return Result.success(new FileUploadDTO(url,path)).msg("上传成功");
    }

    @ApiOperation(value = "删除文件")
    @PostMapping("/deleteFile")
    public Result deleteFile(@RequestParam("文件路径") String fileName){
        ossConfig.deleteObject(fileName);
        return Result.success();
    }

    @ApiOperation(value = "查询文件是否纯在")
    @PostMapping("/doesFile")
    public Result doesFile(@RequestParam("文件路径") String fileName){
        return Result.success("查询成功").data(ossConfig.doesObjectExist(fileName));
    }




}
